﻿Imports DTO
Imports BUS
Imports System.Windows.Forms
Imports System.Reflection

Public Class frmLuuPinNLMT
    Private pinController As New PinBUS
    Public list As New List(Of PinDTO)
    Public listChonSua As New List(Of Integer)
    Private flagIsSuaNhieu As Boolean
    Private flagIsUp As Integer
    Public tracuu As Boolean = False
    Private them As Boolean = False
    Private sua As Boolean = False
    Private xoa As Boolean = False
    Private _trangThaiDangNhap As Boolean
    Public Sub TrangThai(trangthai As Boolean)
        _trangThaiDangNhap = trangthai
    End Sub
    Private Sub BlockButton(ByVal t As Control, ByVal h As Control, ByVal v As Control, ByVal r As Control, ByVal s As Control, ByVal x As Control, Optional ByVal capdo As Byte = 0)
        Select Case capdo
            Case 0
                t.Enabled = True
                h.Enabled = True
                v.Enabled = True
                r.Enabled = True
                s.Enabled = True
                x.Enabled = True

            Case 1
                t.Enabled = True
                h.Enabled = False
                v.Enabled = False
                r.Enabled = False
                s.Enabled = False
                x.Enabled = False
        End Select
    End Sub
    Private Function TestData() As Boolean
        TestData = True
        If txtTenPin.Text = "" Then
            TestData = False
            MsgBox("Tên pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtTenPin.Focus()
            Exit Function
        ElseIf txtKichThuoc.Text = "" Then
            TestData = False
            MsgBox("Kích thước không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtKichThuoc.Focus()
            Exit Function
        ElseIf txtCongSuat.Text = "" Then
            TestData = False
            MsgBox("Công suất không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtCongSuat.Focus()
            Exit Function
        ElseIf txtHangSX.Text = "" Then
            TestData = False
            MsgBox("Hãng sản xuất không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtHangSX.Focus()
            Exit Function
        ElseIf cbLoaiPin.SelectedIndex = -1 Then
            TestData = False
            MsgBox("Loại pin không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            cbLoaiPin.Focus()
            Exit Function
        ElseIf txtImp.Text = "" Then
            TestData = False
            MsgBox("Imp không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtImp.Focus()
            Exit Function
        ElseIf txtVmp.Text = "" Then
            TestData = False
            MsgBox("Vmp không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtVmp.Focus()
            Exit Function
        ElseIf txtIsc.Text = "" Then
            TestData = False
            MsgBox("Isc không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtIsc.Focus()
            Exit Function
        ElseIf txtVoc.Text = "" Then
            TestData = False
            MsgBox("Voc không được bỏ trống!", MsgBoxStyle.Exclamation, "Thông báo lỗi")
            txtVoc.Focus()
            Exit Function
        End If
    End Function
    Private Sub LoadGrid()
        pinController = New PinBUS()
        dgvDuLieuThem.Rows.Clear()
        For i As Integer = 0 To list.Count - 1
            dgvDuLieuThem.Rows.Add()
            dgvDuLieuThem.Rows.Item(i).Cells(0).Value = list(i).MaPin
            dgvDuLieuThem.Rows.Item(i).Cells(1).Value = list(i).TenPin
            dgvDuLieuThem.Rows.Item(i).Cells(2).Value = list(i).MaLoai
            dgvDuLieuThem.Rows.Item(i).Cells(3).Value = list(i).CongSuat
            dgvDuLieuThem.Rows.Item(i).Cells(4).Value = list(i).KichThuoc
            dgvDuLieuThem.Rows.Item(i).Cells(5).Value = list(i).HangSanXuat
            dgvDuLieuThem.Rows.Item(i).Cells(6).Value = list(i).Imp
            dgvDuLieuThem.Rows.Item(i).Cells(7).Value = list(i).Vmp
            dgvDuLieuThem.Rows.Item(i).Cells(8).Value = list(i).Isc
            dgvDuLieuThem.Rows.Item(i).Cells(9).Value = list(i).Voc
        Next
    End Sub
    Public Property ReceiveData() As List(Of PinDTO)
        Get
            Return list
        End Get
        Set(ByVal Value As List(Of PinDTO))
            list = Value
        End Set
    End Property
    Public Sub AddList(ByVal sListSelect As IList(Of Integer))
        For i As Integer = 0 To sListSelect.Count - 1
            Dim j As Integer = Val(sListSelect.Item(i))
            list.AddRange(pinController.SelectById(j))
        Next
        flagIsUp = 2
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInput.Click
        Dim dialog As New OpenFileDialog
        With dialog
            .Filter = "Excel Files (*.xls)|*.xls|All Files (*.*)|*.*"
            .FilterIndex = 1
            .Title = "Open File Dialog"
        End With
        Dim strFileName As String
        strFileName = ""
        If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                strFileName = dialog.FileName
            Catch ex As Exception
                MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
        If Not String.IsNullOrEmpty(strFileName) Then
            PinBUS.ImportExcel(strFileName)
            MessageBox.Show("Đã thêm dữ liệu thành công", "Thông báo", MessageBoxButtons.OK)
        Else
            dialog.Dispose()
        End If

    End Sub

    Private Sub LoadData()
        If tracuu = False Then
            Dim ds As List(Of PinDTO) = BUS.PinBUS.ReadData()
            dgvDuLieuThem.DataSource = ds
        Else
            dgvDuLieuThem.DataSource = list
        End If

    End Sub
    Private Sub LoadLoaiPin()
        cbLoaiPin.DataSource = BUS.PinBUS.ReadLoaiPin().Tables("LoaiPin")
        cbLoaiPin.DisplayMember = "TenLoai"
        cbLoaiPin.ValueMember = BUS.PinBUS.ReadLoaiPin().Tables("LoaiPin").Columns("MaLoai").ToString
    End Sub
    Private Sub frmLuuPinNLMT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim mdiparent As Main = DirectCast(Me.MdiParent, Main)
        If mdiparent.DaDangNhap = False Then
            If MessageBox.Show("Bạn chưa nhập Mật Khẩu để thực hiện chỉnh sửa Cơ Sở Dữ Liệu. Bạn có muốn nhập Mật Khẩu không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                Dim frmMatKhau As New NhapMatKhau()
                frmMatKhau.MdiParent = Me.MdiParent
                Dim result = frmMatKhau.ShowDialog()
                If result = Windows.Forms.DialogResult.Cancel Then
                    Me.Close()
                ElseIf result = Windows.Forms.DialogResult.OK Then

                    mdiparent.DangNhap()
                End If
            Else
                Me.Close()
            End If
        End If
        LoadLoaiPin()
        If list.Count > 0 Then
            LoadGrid()
            BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoa, btnXoaHet, btnLuuTatCa, 0)
        End If
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim MaPin As Integer = 0
        Dim TenPin As String = txtTenPin.Text
        Dim MaLoai As Integer = cbLoaiPin.SelectedValue
        Dim CongSuat As Integer = Val(txtCongSuat.Text)
        Dim KichThuoc As String = txtKichThuoc.Text
        Dim HangSanXuat As String = txtHangSX.Text
        Dim Imp As Integer = Val(txtImp.Text)
        Dim Vmp As Integer = Val(txtVmp.Text)
        Dim Isc As Integer = Val(txtIsc.Text)
        Dim Voc As Integer = Val(txtVoc.Text)

        If TestData() = True Then
            Dim pin As New PinDTO(MaPin, TenPin, MaLoai, CongSuat, KichThuoc, HangSanXuat, Imp, Vmp, Isc, Voc)
            list.Add(pin)
            LoadGrid()
        End If
        BlockButton(btnThem, btnUpdate, btnNhapLai, btnXoa, btnXoaHet, btnLuuTatCa, 0)

        them = True
        'btnLuuTatCa.Enabled = True

        'Dim pin As New PinDTO()
        'pin.TenPin = txtTenPin.Text
        'pin.MaLoai = cbLoaiPin.Text
        'pin.CongSuat = txtCongSuat.Text
        'pin.KichThuoc = txtKichThuoc.Text
        'pin.HangSanXuat = txtHangSX.Text
        'pin.Imp = txtImp.Text
        'pin.Vmp = txtVmp.Text
        'pin.Isc = txtIsc.Text
        'pin.Voc = txtVoc.Text

        'Dim insert As New PinDAO()
        'insert.InsertData(pin.TenPin, pin.MaLoai, pin.CongSuat, pin.KichThuoc, pin.HangSanXuat, pin.Imp, pin.Vmp, pin.Isc, pin.Vmp)
        'LoadData()
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        'Dim pin As New PinDTO()
        'pin.MaPin = BUS.PinBUS.ReadData()(dgvDuLieuThem.CurrentRow.Index).MaPin
        'Try
        '    pin.TenPin = txtTenPin.Text
        '    pin.MaLoai = cbLoaiPin.SelectedValue
        '    pin.CongSuat = Convert.ToInt32(txtCongSuat.Text)
        '    pin.KichThuoc = txtKichThuoc.Text
        '    pin.HangSanXuat = txtHangSX.Text
        '    pin.Imp = Convert.ToInt32(txtImp.Text)
        '    pin.Vmp = Convert.ToInt32(txtVmp.Text)
        '    pin.Isc = Convert.ToInt32(txtIsc.Text)
        '    pin.Voc = Convert.ToInt32(txtVoc.Text)
        'Catch ex As Exception
        '    MessageBox.Show("Dữ liệu nhập sai", "Lỗi", MessageBoxButtons.OK)
        'End Try
        'list.Add(pin)
        'dgvDuLieuThem.DataSource = list
        'sua = True

        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                Dim a As Integer = dgvDuLieuThem.CurrentRow.Index

                If list(a).MaPin = Val(lbMaPin.Text) Then
                    list(a).TenPin = txtTenPin.Text
                    list(a).HangSanXuat = txtHangSX.Text
                    list(a).MaLoai = cbLoaiPin.SelectedValue
                    list(a).CongSuat = Val(txtCongSuat.Text)
                    list(a).KichThuoc = Val(txtKichThuoc.Text)
                    list(a).Imp = Val(txtImp.Text)
                    list(a).Vmp = Val(txtVmp.Text)
                    list(a).Isc = Val(txtIsc.Text)
                    list(a).Voc = Val(txtVoc.Text)
                End If
            Next
        ElseIf dgvDuLieuThem.SelectedRows.Count > 1 Then
            For i = 0 To dgvDuLieuThem.SelectedRows.Count - 1
                listChonSua.Add(Convert.ToInt32(dgvDuLieuThem.SelectedRows(i).Cells("MaPin").Value.ToString()))
            Next
            For j As Integer = 0 To listChonSua.Count - 1
                For i As Integer = 0 To list.Count - 1
                    If listChonSua(j) = list(i).MaPin Then
                        If txtTenPin.Text <> "" Then
                            list(i).TenPin = Val(txtTenPin.Text)
                        End If
                        If txtHangSX.Text <> "" Then
                            list(i).HangSanXuat = txtHangSX.Text
                        End If
                        If txtCongSuat.Text <> "" Then
                            list(i).CongSuat = Val(txtCongSuat.Text)
                        End If
                        If txtKichThuoc.Text <> "" Then
                            list(i).KichThuoc = Val(txtKichThuoc.Text)
                        End If
                        If cbLoaiPin.SelectedIndex <> -1 Then
                            list(i).MaLoai = cbLoaiPin.SelectedValue
                        End If
                        If txtImp.Text <> "" Then
                            list(i).Imp = Val(txtImp.Text)
                        End If
                        If txtVmp.Text <> "" Then
                            list(i).Vmp = Val(txtVmp.Text)
                        End If
                        If txtIsc.Text <> "" Then
                            list(i).Isc = Val(txtIsc.Text)
                        End If
                        If txtVoc.Text <> "" Then
                            list(i).Voc = Val(txtVoc.Text)
                        End If
                    End If
                Next
            Next
            listChonSua.Clear()
        End If
        LoadGrid()
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        btnLuuTatCa.Enabled = True
        txtTenPin.Text = ""
        txtCongSuat.Text = ""
        txtKichThuoc.Text = ""
        txtHangSX.Text = ""
        txtImp.Text = ""
        txtVmp.Text = ""
        txtIsc.Text = ""
        txtVoc.Text = ""
        cbLoaiPin.Text = ""
    End Sub

    Private Sub dgvDuLieuThem_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvDuLieuThem.CellClick
        Dim i As Integer
        i = dgvDuLieuThem.CurrentRow.Index
        If dgvDuLieuThem.SelectedRows.Count = 1 Then
            txtTenPin.Text = dgvDuLieuThem.Item(1, i).Value
            cbLoaiPin.Text = dgvDuLieuThem.Item(2, i).Value
            txtCongSuat.Text = dgvDuLieuThem.Item(3, i).Value
            txtKichThuoc.Text = dgvDuLieuThem.Item(4, i).Value
            txtHangSX.Text = dgvDuLieuThem.Item(5, i).Value
            txtImp.Text = dgvDuLieuThem.Item(6, i).Value
            txtVmp.Text = dgvDuLieuThem.Item(7, i).Value
            txtIsc.Text = dgvDuLieuThem.Item(8, i).Value
            txtVoc.Text = dgvDuLieuThem.Item(9, i).Value
        Else
            txtTenPin.Text = ""
            txtCongSuat.Text = ""
            txtKichThuoc.Text = ""
            txtHangSX.Text = ""
            txtImp.Text = ""
            txtVmp.Text = ""
            txtIsc.Text = ""
            txtVoc.Text = ""
            cbLoaiPin.Text = ""
        End If

    End Sub

    Private Sub cbInputFromFile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbInputFromFile.CheckedChanged
        If cbInputFromFile.Checked = True Then
            txtTenPin.Enabled = False
            cbLoaiPin.Enabled = False
            txtCongSuat.Enabled = False
            txtKichThuoc.Enabled = False
            txtHangSX.Enabled = False
            txtImp.Enabled = False
            txtVmp.Enabled = False
            txtIsc.Enabled = False
            txtVoc.Enabled = False
            btnThem.Enabled = False
            btnNhapLai.Enabled = False
        Else
            txtTenPin.Enabled = True
            cbLoaiPin.Enabled = True
            txtCongSuat.Enabled = True
            txtKichThuoc.Enabled = True
            txtHangSX.Enabled = True
            txtImp.Enabled = True
            txtVmp.Enabled = True
            txtIsc.Enabled = True
            txtVoc.Enabled = True
            btnThem.Enabled = True
            btnNhapLai.Enabled = True
        End If
    End Sub

    Private Sub btnLuuTatCa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuuTatCa.Click
        Dim pin As New PinDTO()
        pin.TenPin = txtTenPin.Text
        pin.MaLoai = cbLoaiPin.SelectedValue
        pin.CongSuat = txtCongSuat.Text
        pin.KichThuoc = txtKichThuoc.Text
        pin.HangSanXuat = txtHangSX.Text
        pin.Imp = txtImp.Text
        pin.Vmp = txtVmp.Text
        pin.Isc = txtIsc.Text
        pin.Voc = txtVoc.Text
        Dim bien As New PinBUS
        If (them = True) Then
            bien.InsertData(pin.TenPin, pin.MaLoai, pin.CongSuat, pin.KichThuoc, pin.HangSanXuat, pin.Imp, pin.Vmp, pin.Isc, pin.Vmp)
            MessageBox.Show("Đã thêm dữ liệu thành công", "Thông báo", MessageBoxButtons.OK)
            them = False
        End If
        If (xoa = True) Then
            bien.DeleteData(list(0).MaPin)
            MessageBox.Show("Đã xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK)
            xoa = False
        End If
        If (sua = True) Then
            bien.UpdateData(list(0).MaPin, list(0).TenPin, list(0).MaLoai, list(0).CongSuat, list(0).KichThuoc, list(0).HangSanXuat, list(0).Imp, list(0).Vmp, list(0).Isc, list(0).Vmp)
            MessageBox.Show("Đã cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK)
            sua = False
        End If
    End Sub

    Private Sub btnXoaHet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaHet.Click
        list.Clear()
        LoadGrid()
    End Sub
    Public Function ToDataTable(Of T)(ByVal list As List(Of T)) As DataTable
        Dim dt As New DataTable()
        If list.Count > 0 Then
            Dim listType As Type = list.ElementAt(0).[GetType]()
            'Get element properties and add datatable columns  
            Dim properties As PropertyInfo() = listType.GetProperties()
            For Each [property] As PropertyInfo In properties
                dt.Columns.Add(New DataColumn() With { _
                 .ColumnName = [property].Name _
                })
            Next
            For Each item As Object In list
                Dim dr As DataRow = dt.NewRow()
                For Each col As DataColumn In dt.Columns
                    dr(col) = listType.GetProperty(col.ColumnName).GetValue(item, Nothing)
                Next
                dt.Rows.Add(dr)
            Next
        End If
        Return dt
    End Function

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        For i As Integer = 0 To list.Count - 1
            If list(i).TenPin = txtTenPin.Text And list(i).MaLoai = cbLoaiPin.SelectedValue Then
                list.RemoveAt(i)
                Exit For
            End If
        Next
        LoadGrid()
    End Sub

End Class
